/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HospedajeLista.java
 *
 * Created on 31/10/2010, 04:12:08 PM
 */

package pe.edu.upla.lamerced.desk.view.hospedaje;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Hospedaje;
import pe.edu.upla.lamerced.bean.Tipohabitacion;
import pe.edu.upla.lamerced.service.HabitacionService;
import pe.edu.upla.lamerced.service.HospedajeService;
import pe.edu.upla.lamerced.service.ServiciosService;

/**
 *
 */
public class HospedajeLista extends javax.swing.JInternalFrame {

    private HospedajeService hospedajeService;
    private ServiciosService serviciosService;
    private HabitacionService habitacionService;
    private JDesktopPane escritorio;

    /** Creates new form HospedajeLista */
    public HospedajeLista() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        cbtipohabitacion = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        btnAnulaHospedaje = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        btnNuevoHospedaje = new javax.swing.JButton();
        listaReservas = new javax.swing.JScrollPane();
        tbListaReservas = new javax.swing.JTable();
        btnFacturaHospedaje = new javax.swing.JButton();

        setClosable(true);
        setTitle("Lista de hospedaje");

        jLabel6.setText("Fecha");

        cbtipohabitacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel4.setText("Lista de Hospedaje");

        btnAnulaHospedaje.setText("Anular");
        btnAnulaHospedaje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnulaHospedajeActionPerformed(evt);
            }
        });

        jLabel2.setText("Tipo de habitacion");

        btnNuevoHospedaje.setText("Nueva");
        btnNuevoHospedaje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoHospedajeActionPerformed(evt);
            }
        });

        tbListaReservas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "Codigo", "Habitacion", "Cliente", "Fecha"
            }
        ));
        listaReservas.setViewportView(tbListaReservas);

        btnFacturaHospedaje.setText("Pre facura");
        btnFacturaHospedaje.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFacturaHospedajeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(25, 25, 25))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(83, 83, 83)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbtipohabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(130, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(177, 177, 177)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(153, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(btnNuevoHospedaje)
                .addGap(30, 30, 30)
                .addComponent(btnFacturaHospedaje)
                .addGap(32, 32, 32)
                .addComponent(btnAnulaHospedaje, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(104, Short.MAX_VALUE))
            .addComponent(listaReservas, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbtipohabitacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFacturaHospedaje)
                    .addComponent(btnAnulaHospedaje, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnNuevoHospedaje))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(listaReservas, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }


    private void btnNuevoHospedajeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoHospedajeActionPerformed
       // TODO add your handling code here:
       HospedajeNuevo rn = new  HospedajeNuevo();
       rn.setEscritorio(this.getEscritorio());
       rn.setVisible(true);
       rn.cargaInicial();
       this.getEscritorio().add(rn);
    }//GEN-LAST:event_btnNuevoHospedajeActionPerformed

    private void btnFacturaHospedajeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFacturaHospedajeActionPerformed
       // TODO add your handling code here:
       if (tbListaReservas.getSelectedRow()!=-1){
          FacturaNueva form = new FacturaNueva();
          form.setEscritorio(getEscritorio());
          int cod = (Integer)tbListaReservas.getValueAt( tbListaReservas.getSelectedRow(), 0);
          form.setHospedaje(hospedajeService.obtenerHospedaje(cod));
          getEscritorio().add(form);
          form.preLoadForm();
          form.setVisible(true);
       }
   
    }//GEN-LAST:event_btnFacturaHospedajeActionPerformed

    private void btnAnulaHospedajeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnulaHospedajeActionPerformed
       //  anular un hospedaje
       if (tbListaReservas.getSelectedRow()!=-1){
          int cod = (Integer)tbListaReservas.getValueAt( tbListaReservas.getSelectedRow(), 0);
          hospedajeService.anularHospedaje(cod);
          JOptionPane.showMessageDialog(this, "Se ha eliminado el hospedaje","Informe",JOptionPane.INFORMATION_MESSAGE);
          pintagrilla();
       }else{
         JOptionPane.showMessageDialog(this, "no ha seleccionado ningun hospedaje","Error",JOptionPane.ERROR_MESSAGE);
       }
    }//GEN-LAST:event_btnAnulaHospedajeActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnulaHospedaje;
    private javax.swing.JButton btnFacturaHospedaje;
    private javax.swing.JButton btnNuevoHospedaje;
    private javax.swing.JComboBox cbtipohabitacion;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JScrollPane listaReservas;
    private javax.swing.JTable tbListaReservas;
    // End of variables declaration//GEN-END:variables

      public void preLoadFormHospLst(){
      if(hospedajeService==null){
        hospedajeService = (HospedajeService)Inicial.getFactory().getBean("hospedajeService");
      }
      if(serviciosService==null){
         serviciosService = (ServiciosService)Inicial.getFactory().getBean("serviciosService");
      }
       if(habitacionService==null){
         habitacionService = (HabitacionService)Inicial.getFactory().getBean("habitacionService");
      }
      cbtipohabitacion.removeAllItems();
      for(Tipohabitacion th : habitacionService.listaTipoHabitacion()){
         cbtipohabitacion.addItem(th.getNombre());
      }
      pintagrilla();
   }
    public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tbListaReservas.getModel();
        tabla.setRowCount(0);
        List<Hospedaje> lhospedajes = hospedajeService.listaHospedajesActivos();
        for (Hospedaje hosp : lhospedajes) {
            Object[] fila = new Object[4];
            fila[0] = hosp.getIdhospedaje();
            fila[1] = hosp.getHabitacion().getNumero();
            fila[2] = hosp.getUsersByIdUsuarioCli().getNombrecompleto();
            fila[3] = hosp.getFecAlquiler();
            tabla.addRow(fila);
        }
    }

}
